/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * DepsXMLConnector.java
 * 
 * Connector for parsing XML files. Implements the interface IDataConnector
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.dataConnector;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

import edu.cmu.isr.dinvenio.fileParsers.DepsXMLParser;
import edu.cmu.isr.dinvenio.model.DepsGraph;

public class DepsXMLFileConnector implements IDataConnector {
	
	private String dataFilePath = null;

	public DepsXMLFileConnector() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void loadDataToModel(Shell shell, DepsGraph graph) {	
		
		// Get the input file name
		FileDialog fd = new FileDialog(shell, SWT.OPEN);
		fd.setText("Open the data xml file");
		if (this.dataFilePath != null ){
			fd.setFilterPath(this.dataFilePath);
		}
		else{
			fd.setFilterPath("C:\\workspace\\edu.cmu.isr.dinvenio\\data");
		}        
        String[] filterExt = { "*.xml" };
        fd.setFilterExtensions(filterExt);
        String sourceFile = fd.open();
        
        // If the source is valid then open the file and parse it
        if (sourceFile != null){            
        	this.dataFilePath = sourceFile;
        	
        	DepsXMLParser depsXMLParser = new DepsXMLParser(graph);
    		SAXParserFactory saxFactory = null;
    		SAXParser parser = null;		
    		try {
    			saxFactory = SAXParserFactory.newInstance();
    			parser = saxFactory.newSAXParser();
    			File inFile = new File(this.dataFilePath);
    			parser.parse(inFile, depsXMLParser);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
        	
    		graph.updateNodeDegrees();        	
        }
	
	}

}
